package com.relytosoft.mizar.analyze.strategy.controller;

import com.alibaba.fastjson.JSON;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.kafka.producer.KafkaOpMsg;
import com.relytosoft.dubhe.common.kafka.producer.KafkaProducer;
import com.relytosoft.mizar.analyze.base.constant.MediaMqConstants;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.*;
import com.relytosoft.mizar.analyze.strategy.algorithm.face.service.FaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Jierry
 * @version 2022/5/11.
 */
@RestController
@RequestMapping("/strategy/face")
@Api(tags = "人脸应用相关接口")
public class FaceController {

    @Resource
    private FaceService faceService;

    @Resource
    private KafkaProducer kafkaProducer;

    @GetMapping("/test")
    public ResponseResult test() {
        return faceService.test();
    }

    @PostMapping(value = "/add")
    @ApiOperation("实时人脸分析任务添加")
    public ResponseResult createTask(@RequestBody TaskRequestDto taskRequestDto) {
        //添加创建任务kafka消息
        KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
        kafkaOpMsg.setOpType("insert");
        kafkaOpMsg.setData(JSON.toJSONString(taskRequestDto));
        kafkaProducer.send(MediaMqConstants.FACE_ANALYZE_TASK_TOPIC, JSON.toJSONString(kafkaOpMsg), true);
        return ResponseResult.ok();
        //直接调用创建任务
        //return faceService.createTask(taskRequestDto);
    }

    @GetMapping(value = "/stop")
    @ApiOperation("实时人脸分析任务删除")
    public ResponseResult stopTask(String taskId) {

        //添加创建任务kafka消息
        KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
        kafkaOpMsg.setOpType("delete");
        kafkaOpMsg.setData(taskId);
        kafkaProducer.send(MediaMqConstants.FACE_ANALYZE_TASK_TOPIC, JSON.toJSONString(kafkaOpMsg), true);
        return ResponseResult.ok();
        //直接调用删除任务
        //return faceService.stopTask(taskId);
    }

    @PostMapping(value = "/fearture/extraction")
    @ApiOperation("人脸分析")
    public ResponseResult feartureExtraction(@RequestBody FeatureExtractionReqDto featureExtractionReqDto) {
        return faceService.feartureExtraction(featureExtractionReqDto);
    }

    @PostMapping(value = "/fearture/comparison")
    @ApiOperation("人脸1:1")
    public ResponseResult feartureComparison(@RequestBody FeatureComparisonReqDto featureComparisonReqDto) {
        return faceService.feartureComparison(featureComparisonReqDto);
    }

    @PostMapping(value = "/group/add")
    @ApiOperation("人脸库添加")
    public ResponseResult createGroup(@RequestBody FaceGroupReqDto faceGroupReqDto) {
        //添加创建人脸库kafka消息
        KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
        kafkaOpMsg.setOpType("insertGroup");
        kafkaOpMsg.setData(JSON.toJSONString(faceGroupReqDto));
        kafkaProducer.send(MediaMqConstants.FACE_ANALYZE_PERSON_TOPIC, JSON.toJSONString(kafkaOpMsg), true);
        return ResponseResult.ok();
        //return faceService.createGroup(faceGroupReqDto);
    }

    @PostMapping(value = "/group/delete")
    @ApiOperation("人脸库删除")
    public ResponseResult deleteGroup(@RequestBody FaceGroupReqDto faceGroupReqDto) {
        //添加删除人脸库kafka消息
        KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
        kafkaOpMsg.setOpType("deleteGroup");
        kafkaOpMsg.setData(JSON.toJSONString(faceGroupReqDto));
        kafkaProducer.send(MediaMqConstants.FACE_ANALYZE_PERSON_TOPIC, JSON.toJSONString(kafkaOpMsg), true);
        return ResponseResult.ok();
        //return faceService.deleteGroup(faceGroupReqDto);
    }

    @PostMapping(value = "/person/add")
    @ApiOperation("人脸添加")
    public ResponseResult createPerson(@RequestBody FacePersonReqDto facePersonReqDto) {
        //添加创建人脸kafka消息
        KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
        kafkaOpMsg.setOpType("insertPerson");
        kafkaOpMsg.setData(JSON.toJSONString(facePersonReqDto));
        kafkaProducer.send(MediaMqConstants.FACE_ANALYZE_PERSON_TOPIC, JSON.toJSONString(kafkaOpMsg), true);
        return ResponseResult.ok();
        //return faceService.createPerson(facePersonReqDto);
    }

    @PostMapping(value = "/person/delete")
    @ApiOperation("人脸删除")
    public ResponseResult deletePerson(@RequestBody FacePersonReqDto facePersonReqDto) {
        //添加删除人脸kafka消息
        KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
        kafkaOpMsg.setOpType("deletePerson");
        kafkaOpMsg.setData(JSON.toJSONString(facePersonReqDto));
        kafkaProducer.send(MediaMqConstants.FACE_ANALYZE_PERSON_TOPIC, JSON.toJSONString(kafkaOpMsg), true);
        return ResponseResult.ok();
        //return faceService.deletePerson(facePersonReqDto);
    }

    @PostMapping(value = "/person/search")
    @ApiOperation("人脸检索")
    public ResponseResult deletePerson(@RequestBody FaceSearchReqDto faceSearchReqDto) {
        return faceService.searchPerson(faceSearchReqDto);
    }

    @PostMapping(value = "/disposition/add")
    @ApiOperation("人脸布控任务创建")
    public ResponseResult createDisposition(@RequestBody DispositionReqDto dispositionReqDto) {
        //添加创建人脸布控任务kafka消息
        KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
        kafkaOpMsg.setOpType("insert");
        kafkaOpMsg.setData(JSON.toJSONString(dispositionReqDto));
        kafkaProducer.send(MediaMqConstants.FACE_DISPOSITION_TOPIC, JSON.toJSONString(kafkaOpMsg), true);
        return ResponseResult.ok();
        //return faceService.createDispostion(dispositionReqDto);
    }

    @PostMapping(value = "/disposition/delete")
    @ApiOperation("人脸布控任务删除")
    public ResponseResult deleteDisposition(String dispositionId) {
        //添加删除人脸布控任务kafka消息
        KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
        kafkaOpMsg.setOpType("delete");
        kafkaOpMsg.setData(dispositionId);
        kafkaProducer.send(MediaMqConstants.FACE_DISPOSITION_TOPIC, JSON.toJSONString(kafkaOpMsg), true);
        return ResponseResult.ok();
        //return faceService.deleteDispostion(dispositionId);
    }
}
